package com.penghaisoft.microwms.product.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.ProContractChangePlanDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.MSBarCode;
import com.penghaisoft.microwms.common.wms.model.ProContractChange;
import com.penghaisoft.microwms.common.wms.model.ProContractChangePlan;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.dao.MSBarCodeMapper;
import com.penghaisoft.microwms.product.dao.ProContractChangeMapper;
import com.penghaisoft.microwms.product.dao.ProContractChangePlanMapper;
import com.penghaisoft.microwms.product.dao.ProStorageSnMapper;
import com.penghaisoft.microwms.product.service.IProContractChangePlanService;

/**
 * @Package com.penghaisoft.microwms.product.service.impl
 * @ClassName: ProContractChangePlanServiceImpl
 * @Description: 成品合同变更计划表
 * @author luoteng
 * @date 2019年3月6日 上午9:49:55
 */
@Service("proContractChangePlanService")
public class ProContractChangePlanServiceImpl extends BaseService implements IProContractChangePlanService {
	@Resource
	private ProContractChangePlanMapper proContractChangePlanMapper;

	@Resource
	private ProStorageSnMapper proStorageSnMapper;

	@Resource
	private ProContractChangeMapper proContractChangeMapper;

	@Resource
	private MSBarCodeMapper mSBarCodeMapper;

	/**
	 * 新增记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp create(ProContractChangePlan proContractChangePlan) {
		proContractChangePlanMapper.create(proContractChangePlan);
		return success();
	}

	/**
	 * 删除记录（逻辑删除）
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp delete(ProContractChangePlan proContractChangePlan) {
		proContractChangePlanMapper.delete(proContractChangePlan);
		return success();
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @param baseResource
	 * @return
	 */
	@Override
	public Pager<ProContractChangePlan> findListByCondition(int page, int rows, ProContractChangePlan condition) {
		Pager<ProContractChangePlan> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.preQuery();
		List<ProContractChangePlan> records = proContractChangePlanMapper.queryList(pager, condition);
		long size = proContractChangePlanMapper.queryCount(condition);
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public ProContractChangePlan findById(String id) {
		return proContractChangePlanMapper.queryById(id);
	}

	/**
	 * 修改记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp update(ProContractChangePlan proContractChangePlan) {
		proContractChangePlanMapper.updateBySelect(proContractChangePlan);
		return success();
	}

	/**
	 * 修改记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp updateNew(ProContractChangePlan proContractChangePlan) {
		proContractChangePlanMapper.updateBySelectNew(proContractChangePlan);
		return success();
	}

	@Override
	@Transactional
	public Resp insert(List<ProContractChangePlan> list, String contractChangePlanNo, String loginName) {
		Date now = new Date();
		HashSet<String> allSet = new HashSet<String>();
		for (int i = 0; i < list.size(); i++) {
			ProContractChangePlan tmp = list.get(i);

			StringBuffer uniqueStr = new StringBuffer("");
			uniqueStr.append(tmp.getModel());
			uniqueStr.append(tmp.getModelType());
			if (allSet.contains(uniqueStr.toString())) {
				Resp resp = new Resp();
				resp.setCode(Constant.FAIL);
				resp.setMsg("选择的型号、型号类型重复");
				return resp;
			} else {
				allSet.add(uniqueStr.toString());
			}
		}

		for (int i = 0; i < list.size(); i++) {
			ProContractChangePlan proContractChangePlan = list.get(i);

//	    		原合同编号、新合同编号、型号、型号类型、需求数量、是否需要归还 页面传递的参数已经提供
			proContractChangePlan.setContractChangePlanNo(contractChangePlanNo);
			proContractChangePlan.setActiveFlag("1");
			proContractChangePlan.setGmtCreate(now);
			proContractChangePlan.setCreateBy(loginName);
			// 打印状态默认设为0，未打印
			proContractChangePlan.setUserDefined2("0");
//				是否需要归还： 1：需要 2：不需要
			if (proContractChangePlan.getBackFlag() != null && "1".equals(proContractChangePlan.getBackFlag())) {
//					归还状态： 0：未归还 1：部分归还 2：已经归还
				proContractChangePlan.setBackStatus("0");
			}
//				状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			proContractChangePlan.setStatus("0");

			proContractChangePlanMapper.create(proContractChangePlan);
		}

		return success();
	}

	@Override
	public Resp updateByPlanNo(ProContractChangePlan proContractChangePlan) {
		proContractChangePlanMapper.updateByPlanNo(proContractChangePlan);
		return success();
	}

	@Override
	public List<ProContractChangePlan> queryListByAny(ProContractChangePlan condition) {
		return proContractChangePlanMapper.queryByAny(condition);
	}

	@Override
	@Transactional
	public Resp backUpdate(ProContractChangePlan proContractChangePlan, String contractChangePlanNo, String loginName) {
		ProContractChangePlan ob = new ProContractChangePlan();
		ob.setActiveFlag("1");
		ob.setProContractChangePlanId(proContractChangePlan.getProContractChangePlanId());

		List<ProContractChangePlan> list = proContractChangePlanMapper.queryByAny(ob);
		if (list != null && !list.isEmpty()) {
			ProContractChangePlan tmp = list.get(0);

			ProContractChangePlan newPlan = new ProContractChangePlan();
			newPlan.setRelationPlanId(proContractChangePlan.getProContractChangePlanId());

//	    		原合同编号、新合同编号、型号、型号类型、需求数量、是否需要归还 页面传递的参数已经提供
			newPlan.setContractNoOld(proContractChangePlan.getContractNoOld());
			newPlan.setContractNoNew(proContractChangePlan.getContractNoNew());
			newPlan.setModel(proContractChangePlan.getModel());
			newPlan.setModelType(proContractChangePlan.getModelType());
			newPlan.setModelDesc(proContractChangePlan.getModelDesc());
			newPlan.setRequireAmount(proContractChangePlan.getRequireAmount());

			newPlan.setUnit(tmp.getUnit());

//				状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			newPlan.setStatus("0");
			newPlan.setContractChangePlanNo(contractChangePlanNo);
			newPlan.setActiveFlag("1");
			newPlan.setGmtCreate(new Date());
			newPlan.setCreateBy(loginName);
//				变更类型：1：借出 2：归还
			newPlan.setChangeType("2");

			proContractChangePlanMapper.create(newPlan);
		}

		return success();
	}

	/**
	 * @Title: queryPlanList
	 * @Description: 成品合同变更计划查询
	 * @date 2019年3月7日 上午10:47:10
	 * @author luoteng
	 * @param condition
	 * @return List<ProContractChangePlan>
	 */
	@Override
	public List<ProContractChangePlan> queryPlanList(ProContractChangePlan condition) {
		return proContractChangePlanMapper.queryByAny(condition);
	}

	/**
	 * @Title: planScanBarcode
	 * @Description: 成品合同变更扫码
	 * @date 2019年3月7日 上午11:10:21
	 * @author luoteng
	 * @param proContractChangePlanDto
	 * @return TransResult
	 */
	@Transactional
	@Override
	public TransResult planScanBarcode(ProContractChangePlanDto proContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();

		ProStorageSn proStorageSn = new ProStorageSn();
		proStorageSn.setBarcode(proContractChangePlanDto.getBarcode());
		proStorageSn.setActiveFlag("1");

		List<ProStorageSn> storageList = proStorageSnMapper.queryByAny(proStorageSn);
		if (storageList == null || storageList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码不存在！");
			return result;
		} else if (storageList.size() > 1) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码重复！");
			return result;
		} else {
			ProStorageSn storageSnTmp = storageList.get(0);

//	        	0：可用 1：冻结 2：已出库 3：抽检冻结
			if (storageSnTmp.getBarcodeStatus() == null) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码状态为空！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("2")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已出库！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("1") || storageSnTmp.getBarcodeStatus().equals("3")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已冻结！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("0")) {
				ProContractChange proContractChangePlanSn = new ProContractChange();
				proContractChangePlanSn.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
				proContractChangePlanSn.setBarcode(proContractChangePlanDto.getBarcode());
				proContractChangePlanSn.setActiveFlag("1");

				List<ProContractChange> snList = proContractChangeMapper.queryByAny(proContractChangePlanSn);
				if (snList != null && !snList.isEmpty()) {
					result.setCode(Constant.FAIL);
					result.setMsg("该条码已经扫描，不能再次扫描!");
					return result;
				} else {
					ProContractChangePlan proContractChangePlan = new ProContractChangePlan();
					proContractChangePlan.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
					proContractChangePlan.setModel(storageSnTmp.getModel());
					proContractChangePlan.setModelType(storageSnTmp.getModelType());
					proContractChangePlan.setActiveFlag("1");

					List<ProContractChangePlan> planList = proContractChangePlanMapper
							.queryByAny(proContractChangePlan);
					if (planList == null || planList.isEmpty()) {
						result.setCode(Constant.FAIL);
						result.setMsg("该条码不是变更单所需型号!");
						return result;
					} else {
						ProContractChangePlan planTmp = planList.get(0);

//							变更类型：1：借出 2：归还
						String changeType = planTmp.getChangeType();

						if (!planTmp.getContractNoOld().equals(storageSnTmp.getContractNo())) {
							result.setCode(Constant.FAIL);
							result.setMsg("该条码合同号（" + storageSnTmp.getContractNo() + "）与该变更单所需合同号（"
									+ planTmp.getContractNoOld() + "）不相同!");
							return result;
						}
						List<ProContractChangePlan> rtnList = new ArrayList<>();

						// 需求数量
						BigDecimal requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
								: planTmp.getRequireAmount();
						// 已发数量
						BigDecimal realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO : planTmp.getAmount();

						BigDecimal stgamount = storageSnTmp.getAmount() == null ? BigDecimal.ZERO
								: storageSnTmp.getAmount();

						BigDecimal val = (realAmount.add(stgamount)).subtract(requireAmount);
						int b = val.compareTo(Constant.PRO_MAX_STATIC_METER);

						Boolean typeFlag = isProLength(storageSnTmp.getModelType());

						if (realAmount.compareTo(requireAmount) == 0 || requireAmount.compareTo(realAmount) < 0) {
							result.setCode(Constant.FAIL);
							result.setMsg("该型号已经扫码完成，无需继续扫码!");
							return result;
						} else if (typeFlag && b > 0) {
							result.setCode(Constant.FAIL);
							result.setMsg("该物料长度过长(实发数量只允许最多超过需求数量" + Constant.PRO_MAX_STATIC_METER + "米)!");
							return result;
						} else {
//								状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
							planTmp.setStatus("3");
							planTmp.setLastModifiedBy(proContractChangePlanDto.getLoginName());
							planTmp.setGmtModified(now);
//								更新数量的sql 去掉Amout的更新
							planTmp.setAmount(null);
							planTmp.setAmountAdd(storageSnTmp.getAmount());

							realAmount = realAmount.add(storageSnTmp.getAmount());
						}

						Integer rtVal = proContractChangePlanMapper.updateBySelect(planTmp);
						if (rtVal != 1) {
							throw new RuntimeException("扫描条码失败，更新条数存在问题!");
						}

						if ("2".equals(changeType)) {
							ProContractChangePlan guihuan = new ProContractChangePlan();
							guihuan.setProContractChangePlanId(planTmp.getRelationPlanId());

							List<ProContractChangePlan> tmpLLList = proContractChangePlanMapper.queryByAny(guihuan);
							if (planList != null && !planList.isEmpty()) {
								ProContractChangePlan tmpYZOb = tmpLLList.get(0);

								// 借出数量
								BigDecimal amount = tmpYZOb.getAmount() == null ? BigDecimal.ZERO : tmpYZOb.getAmount();

								// 归还数量
								BigDecimal backAmount = tmpYZOb.getBackAmount() == null ? BigDecimal.ZERO
										: tmpYZOb.getBackAmount();
								backAmount = backAmount.add(storageSnTmp.getAmount());

								if (backAmount.compareTo(amount) == 0) {
//										归还状态： 0：未归还 1：部分归还 2：已经归还
									guihuan.setBackStatus("2");
								} else {
									guihuan.setBackStatus("1");
								}
							}

							guihuan.setBackAmountAdd(storageSnTmp.getAmount());

//								归还扫码时，关联的借出计划里面退回数量+1
							proContractChangePlanMapper.updateBySelect(guihuan);
						}

//							手持显示 该条码长度
						planTmp.setAmount(storageSnTmp.getAmount());
//							返回给手持数据
						rtnList.add(planTmp);
						result.setList(rtnList);

//							ProContractChangePlan yzOb = new ProContractChangePlan();
//							yzOb.setProContractChangePlanId(planTmp.getProContractChangePlanId());
//							List<ProContractChangePlan> yzList = proContractChangePlanMapper.queryByAny(yzOb);
//							if(yzList != null && !yzList.isEmpty()) {
//								// 需求数量
//								requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
//										: planTmp.getRequireAmount();
//								// 已发数量
//								realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO
//										: planTmp.getAmount();
//								if (requireAmount.compareTo(realAmount) < 0) {
//									throw new RuntimeException("该型号已经扫码完成，无需继续扫码!");
//								}
//							}

						ProContractChange snInsertOb = new ProContractChange();
						snInsertOb.setContractChangePlanId(planTmp.getProContractChangePlanId());
						snInsertOb.setContractChangePlanNo(planTmp.getContractChangePlanNo());
						snInsertOb.setContractNoNew(planTmp.getContractNoNew());
						snInsertOb.setContractNoOld(planTmp.getContractNoOld());
						snInsertOb.setBarcode(proContractChangePlanDto.getBarcode());
						snInsertOb.setModel(planTmp.getModel());
						snInsertOb.setModelDesc(planTmp.getModelDesc());
						snInsertOb.setModelType(planTmp.getModelType());
						snInsertOb.setType(planTmp.getType());
						snInsertOb.setAmount(storageSnTmp.getAmount());
						snInsertOb.setUnit(storageSnTmp.getUnit());
						snInsertOb.setWlCode(storageSnTmp.getWlCode());
						snInsertOb.setActiveFlag("1");
						snInsertOb.setGmtCreate(now);
						snInsertOb.setCreateBy(proContractChangePlanDto.getLoginName());

						proContractChangeMapper.create(snInsertOb);

//							List<ProHandInSn> handInSnYzList = proHandInSnMapper.queryByAny(proHandInSn);
//							if(handInSnYzList.size() > 1){
//								throw new RuntimeException();
//							}

						storageSnTmp.setContractNo(planTmp.getContractNoNew());
						storageSnTmp.setLastModifiedBy(proContractChangePlanDto.getLoginName());
						storageSnTmp.setGmtModified(now);

						proStorageSnMapper.updateBySelect(storageSnTmp);
					}
				}

			}
		}

		return result;
	}

	/**
	 * @Title: planDelBarcode
	 * @Description: 成品合同变更退扫
	 * @date 2019年3月7日 上午11:13:33
	 * @author luoteng
	 * @param proContractChangePlanDto
	 * @return TransResult
	 */
	@Transactional
	@Override
	public TransResult planDelBarcode(ProContractChangePlanDto proContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();

		ProStorageSn ProStorageSn = new ProStorageSn();
		ProStorageSn.setBarcode(proContractChangePlanDto.getBarcode());
		ProStorageSn.setActiveFlag("1");

		List<ProStorageSn> storageList = proStorageSnMapper.queryByAny(ProStorageSn);
		if (storageList == null || storageList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码不存在！");
			return result;
		} else if (storageList.size() > 1) {
			result.setCode(Constant.FAIL);
			result.setMsg("该条码重复!");
			return result;
		} else {
			ProStorageSn storageSnTmp = storageList.get(0);

			if (storageSnTmp.getBarcodeStatus() == null) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码状态为空！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("2")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已出库！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("1") || storageSnTmp.getBarcodeStatus().equals("3")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该条码已冻结！");
				return result;
			} else if (storageSnTmp.getBarcodeStatus().equals("0")) {
				ProContractChange planSn = new ProContractChange();
				planSn.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
				planSn.setBarcode(proContractChangePlanDto.getBarcode());
				planSn.setActiveFlag("1");

				List<ProContractChange> snList = proContractChangeMapper.queryByAny(planSn);

				if (snList == null || snList.isEmpty()) {
					result.setCode(Constant.FAIL);
					result.setMsg("该条码未扫描!");
					return result;
				} else {
					ProContractChange snTmp = snList.get(0);

					ProContractChangePlan proContractChangePlan = new ProContractChangePlan();
					proContractChangePlan.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
					proContractChangePlan.setModel(storageSnTmp.getModel());
					proContractChangePlan.setModelType(storageSnTmp.getModelType());
					proContractChangePlan.setActiveFlag("1");

					List<ProContractChangePlan> planList = proContractChangePlanMapper
							.queryByAny(proContractChangePlan);
					if (planList == null || planList.isEmpty()) {
						result.setCode(Constant.FAIL);
						result.setMsg("条码型号与变更单所需型号不一致!");
						return result;
					} else {
						ProContractChangePlan planTmp = planList.get(0);

//							变更类型：1：借出 2：归还
						String changeType = planTmp.getChangeType();

						List<ProContractChangePlan> rtnList = new ArrayList<>();

						// 需求数量
						BigDecimal requireAmount = planTmp.getRequireAmount() == null ? BigDecimal.ZERO
								: planTmp.getRequireAmount();
						// 已发数量
						BigDecimal realAmount = planTmp.getAmount() == null ? BigDecimal.ZERO : planTmp.getAmount();

//							状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
						planTmp.setStatus("3");
						planTmp.setLastModifiedBy(proContractChangePlanDto.getLoginName());
						planTmp.setGmtModified(now);
//							更新数量的sql 去掉Amout的更新
						planTmp.setAmount(null);

						planTmp.setAmountDel(storageSnTmp.getAmount());
						realAmount = realAmount.subtract(storageSnTmp.getAmount());

						Integer rtVal = proContractChangePlanMapper.updateBySelect(planTmp);
						if (rtVal != 1) {
							throw new RuntimeException("删除条码失败，更新条数存在问题!");
						}

						if ("2".equals(changeType)) {
							ProContractChangePlan guihuan = new ProContractChangePlan();
							guihuan.setProContractChangePlanId(planTmp.getRelationPlanId());

							List<ProContractChangePlan> tmpLLList = proContractChangePlanMapper.queryByAny(guihuan);
							if (planList != null && !planList.isEmpty()) {
								ProContractChangePlan tmpYZOb = tmpLLList.get(0);

								// 归还数量
								BigDecimal backAmount = tmpYZOb.getBackAmount() == null ? BigDecimal.ZERO
										: tmpYZOb.getBackAmount();
								backAmount = backAmount.subtract(new BigDecimal(1));

								if (backAmount.compareTo(BigDecimal.ZERO) == 0) {
//										归还状态： 0：未归还 1：部分归还 2：已经归还
									guihuan.setBackStatus("0");
								} else {
									guihuan.setBackStatus("1");
								}
							}

							guihuan.setBackAmountDel(storageSnTmp.getAmount());

//								归还退扫时，关联的借出计划里面退回数量-1
							proContractChangePlanMapper.updateBySelect(guihuan);
						}

//							手持显示 该条码长度
						planTmp.setAmount(storageSnTmp.getAmount());
//							返回给手持数据
						rtnList.add(planTmp);
						result.setList(rtnList);

						snTmp.setActiveFlag("0");
						snTmp.setLastModifiedBy(proContractChangePlanDto.getLoginName());
						snTmp.setGmtModified(now);

						Integer rtVal1 = proContractChangeMapper.updateBySelect(snTmp);
						if (rtVal1 != 1) {
							throw new RuntimeException("删除条码失败，更新条数存在问题!");
						}

						storageSnTmp.setContractNo(planTmp.getContractNoOld());
						storageSnTmp.setLastModifiedBy(proContractChangePlanDto.getLoginName());
						storageSnTmp.setGmtModified(now);

						proStorageSnMapper.updateBySelect(storageSnTmp);
					}
				}
			}
		}

		return result;
	}

	/**
	 * @Title: planComplete
	 * @Description: 成品合同变更确认完成
	 * @date 2019年3月7日 上午11:16:02
	 * @author luoteng
	 * @param proContractChangePlanDto
	 * @return TransResult
	 */
	@Transactional
	@Override
	public TransResult planComplete(ProContractChangePlanDto proContractChangePlanDto) {
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);

		Date now = new Date();

		ProContractChangePlan proContractChangePlan = new ProContractChangePlan();
		proContractChangePlan.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
		proContractChangePlan.setActiveFlag("1");

		List<ProContractChangePlan> planList = proContractChangePlanMapper.queryByAny(proContractChangePlan);
		if (planList == null || planList.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该变更单不存在!");
			return result;
		} else {
			for (ProContractChangePlan tmp : planList) {
				// 需求数量
				BigDecimal requireAmount = tmp.getRequireAmount() == null ? BigDecimal.ZERO : tmp.getRequireAmount();
				// 已发数量
				BigDecimal realAmount = tmp.getAmount() == null ? BigDecimal.ZERO : tmp.getAmount();

//					BigDecimal maxRealAmout = Constant.PRO_JY_STATIC_METER.add(requireAmount);

				if (requireAmount.compareTo(realAmount) > 0) {
					result.setCode(Constant.FAIL);
					result.setMsg("该变更单未扫码完毕!");
					return result;
				}
//					else if (maxRealAmout.compareTo(realAmount) < 0) {
//						result.setCode(Constant.FAIL);
//						result.setMsg("该变更单扫码数量超过需求数量!");
//						return result;
//					}
			}

			ProContractChangePlan upOb = new ProContractChangePlan();
			upOb.setContractChangePlanNo(proContractChangePlanDto.getContractChangePlanNo());
//				状态：0：创建 1：通过 2：驳回 3：扫码中 4：扫码完成 5：确认完成
			upOb.setStatus("5");
			upOb.setLastModifiedBy(proContractChangePlanDto.getLoginName());
			upOb.setGmtModified(now);

			proContractChangePlanMapper.updateByPlanNo(upOb);
		}

		return result;
	}

	/**
	 * @Title: makeNewBarcode
	 * @Description: 成品合同借用重新生成条码
	 * @author Lengxf
	 * @date 2019年3月28日
	 */
	@Override
	@Transactional
	public Resp makeNewBarcode(ProContractChangePlan proContractChangePlan) {
		Date now = new Date();
		Resp resp = new Resp();

		boolean printfFlag = false;
		List<ProContractChangePlan> listIsPrintfOrNo = proContractChangePlanMapper.queryByAny(proContractChangePlan);
		if (listIsPrintfOrNo != null && listIsPrintfOrNo.size() > 0) {
			ProContractChangePlan isPrintfOrNo = listIsPrintfOrNo.get(0);
			// 判断是否打印 0：未打印 1：已打印
			if (isPrintfOrNo.getUserDefined2() != null && isPrintfOrNo.getUserDefined2().equals("1")) {
				resp.setCode(Constant.FAIL);
				resp.setMsg("该借用合同下的该物料的条码已打印！");
				return resp;
			} else {
				printfFlag = true;
			}
		}

		if (printfFlag) {
			ProContractChange entity = new ProContractChange();
			entity.setContractNoNew(proContractChangePlan.getContractNoNew());
			entity.setModel(proContractChangePlan.getModel());
			List<ProContractChange> records = proContractChangeMapper.queryByAny(entity);
			MSBarCode msBarCode = new MSBarCode();

			if (records != null && records.size() > 0) {
				Integer num = 0;
				for (ProContractChange tmp : records) {
					msBarCode.setBarCode(tmp.getBarcode());
					msBarCode.setModelCode(tmp.getModel());
					msBarCode.setModelType(tmp.getModelType());
					msBarCode.setContractNo(tmp.getContractNoNew());
					msBarCode.setUpdateDate(now);
					msBarCode.setBatchCode(proContractChangePlan.getContractChangePlanNo());
//						产品类型：0：原材料 1：成品
					msBarCode.setProductType("1");
//						条码打印接口表
					mSBarCodeMapper.insertSelective(msBarCode);
					num++;
				}

				proContractChangePlan.setUserDefined2("1");
				proContractChangePlanMapper.updateByModelNo(proContractChangePlan);

				resp.setCode(Constant.SUCCESS);
				resp.setMsg("重新生成了" + num + "个条码，请在打印机进行打印~");

			} else {
				resp.setCode(Constant.FAIL);
				resp.setMsg("参数传递错误");
				return resp;
			}
		}
		return resp;
	}
}
